package com.example.leetdemo.finash;

public class Solution2391 {
    public int garbageCollection(String[] garbage, int[] travel) {
        int[] end = new int[3];
        for (int i = garbage.length - 1; i >= 0; i--) {
            if (garbage[i].contains("M") && end[0] == 0) {
                end[0] = i;
            }
            if (garbage[i].contains("P") && end[1] == 0) {
                end[1] = i;
            }
            if (garbage[i].contains("G") && end[2] == 0) {
                end[2] = i;
            }
            if (end[0] > 0 && end[1] > 0 && end[2] > 0) {
                break;
            }
        }
        int count = 0;
        for (int i = 0; i < garbage.length; i++) {
            count += garbage[i].length();
            if (i > 0) {
                for (int j = 0; j < 3; j++) {
                    if (end[j] >= i) {
                        count += travel[i - 1];
                    }
                }
            }
        }
        return count;
    }
}
